import 'package:flutter/material.dart';
import '../game/snake.dart';
import '../game/food.dart';

class GameBoard extends StatelessWidget {
  final Snake snake;
  final Food food;
  final int gridSize;
  final bool isGameOver;
  
  const GameBoard({
    super.key,
    required this.snake,
    required this.food,
    required this.gridSize,
    required this.isGameOver,
  });
  
  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 1,
      child: Container(
        color: Colors.black,
        child: GridView.builder(
          physics: const NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: gridSize,
          ),
          itemCount: gridSize * gridSize,
          itemBuilder: (context, index) {
            final x = index % gridSize;
            final y = index ~/ gridSize;
            
            // 绘制蛇
            if (snake.body.any((point) => point.x == x && point.y == y)) {
              final isHead = snake.body.first.x == x && snake.body.first.y == y;
              return Container(
                margin: const EdgeInsets.all(1),
                decoration: BoxDecoration(
                  color: isHead ? Colors.green : Colors.lightGreen,
                  borderRadius: BorderRadius.circular(isHead ? 4 : 2),
                ),
              );
            }
            
            // 绘制食物
            if (food.position.x == x && food.position.y == y) {
              return Container(
                margin: const EdgeInsets.all(2),
                decoration: const BoxDecoration(
                  color: Colors.red,
                  shape: BoxShape.circle,
                ),
              );
            }
            
            // 空白格子
            return Container(
              margin: const EdgeInsets.all(0.5),
              color: Colors.grey[900],
            );
          },
        ),
      ),
    );
  }
}